package com.lbx.dao4;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author 吕兵星
 * @date 2020/10/21 17:13
 */
public class ProxyInvocationHandler implements InvocationHandler {
    /**被代理接口对象**/
    private Object target;
    /**
     * 生成代理类
     */
    public void setTarget(Object target) {
        this.target = target;
    }
    public Object getProxy(){
        Object instance = Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
        return instance;

    }
    /**
     * 处理代理实例 并返回结果
     * */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object invoke = method.invoke(target, args);
        log(method.getName());
        return invoke;
    }

    public void  log(String msg){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String date = simpleDateFormat.format(new Date());
        System.out.println("[DEBUG]"+date+"    使用了方法"+msg);
    }
}
